// 源插件
import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import router from 'umi/router';

// 视图插件
import {
  Spin,
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  Dropdown,
  Menu,
  InputNumber,
  DatePicker,
  Modal,
  message,
  Badge,
  Divider,
  Steps,
  Radio,
  Upload,
} from 'antd';

// 外部组件
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import { empty } from '../../../function';

// css OR less预编译器

// 图片

const FormItem = Form.Item;
const { Option } = Select;

/**
 *  Created by xjc on 2019/8/15
 *  Describe:数据连接
 */

@connect(({ foreign, loading }) => ({
  foreign,
  loading: loading.models.foreignRule,
}))
@Form.create()
class AddPositionModal extends PureComponent {
  componentWillMount() {}

  /**
   *  Created by xjc on 2019/8/15
   *  Describe:当前状态
   */

  state = {
    confirmLoading: false,
  };

  onChange = e => {};

  okHandle() {
    const that = this;
    that.setState({
      confirmLoading: true,
    });
    return new Promise((resolve, reject) => {
      setTimeout(resolve, 800);
    })
      .then(() => {
        that.setState({
          confirmLoading: false,
        });
        that.props.form.validateFields((err, values) => {
          if (!err) {
            const { dispatch } = that.props;
            dispatch({
              type: 'foreign/AddExchangePosition',
              payload: {
                bank: empty(values.bank)?null:parseInt(values.bank),
                closePositionDate: empty(values.closePositionDate)?null:values.closePositionDate.format('YYYY-MM-DD'),
                closePositionPrice: empty(values.closePositionPrice)?null:Number(values.closePositionPrice),
                coin: empty(values.coin)?null:Number(values.coin),
                currency: empty(values.currency)?null:values.currency,
                latestPrice: empty(values.latestPrice)?null:Number(values.latestPrice),
                profit: empty(values.profit)?null:Number(values.profit),
                receiptPrice: empty(values.receiptPrice)?null:Number(values.receiptPrice),
                receiptDate: empty(values.receiptDate)?null:values.receiptDate.format('YYYY-MM-DD'),
              },
              callback: res => {
                that.props.closeVisbile();
              },
            });
          }else{
            message.error(err)
            }
          });
      })
      .catch(() => console.log('Oops errors!'));
  }

  cancelHandle() {
    this.props.closeVisbile();
  }

  render() {
    const { confirmLoading } = this.state;
    const { visible, form } = this.props;
    const { getFieldDecorator } = form;

    return (
      <Modal
        destroyOnClose
        title="添加"
        visible={visible}
        width="80%"
        centered
        onOk={this.okHandle.bind(this)}
        confirmLoading={confirmLoading}
        onCancel={this.cancelHandle.bind(this)}
      >
        <Form onSubmit={this.handleSearch} style={{ overflowY: 'scroll' }}>
          <Row>
            <Col >
              <FormItem label="币种" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {getFieldDecorator('currency', { initialValue: '', })(
                  <Input style={{ width: '85%' }} placeholder="币种"/>
                )}
              </FormItem>
              <FormItem label="银行" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {getFieldDecorator('bank', {initialValue: '',
                })(
                  <Select style={{ width: '85%' }}>
                    <Option value="1">瑞银</Option>
                    <Option value="2">中银</Option>
                    <Option value="3">花旗</Option>
                  </Select>
                )}
              </FormItem>

              <FormItem label="金额USD" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {getFieldDecorator('coin', { initialValue: '', })(
                  <Input style={{ width: '85%' }} placeholder="金额USD" />
                )}
              </FormItem>

              <FormItem label="现价" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {getFieldDecorator('latestPrice', { initialValue: '', })(
                  <Input style={{ width: '85%' }} placeholder="现价" />
                )}
              </FormItem>
              <FormItem label="盈亏USD " labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {getFieldDecorator('profit', { initialValue: '', })(
                  <Input style={{ width: '85%' }} placeholder="盈亏USD " />
                )}
              </FormItem>
              <FormItem label="接货日期" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {form.getFieldDecorator('receiptDate', {initialValue: '',
                })(
                  <DatePicker
                    style={{ width: '85%' }}
                    // showTime
                    format="YYYY-MM-DD"
                    placeholder="选择接货日期"
                  />
                )}
              </FormItem>
              <FormItem label="接货价 " labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {getFieldDecorator('receiptPrice', { initialValue: '', })(
                  <Input style={{ width: '85%' }} placeholder="接货价 " />
                )}
              </FormItem>
              <FormItem label="平仓日期" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {form.getFieldDecorator('closePositionDate', {initialValue: '',
                })(
                  <DatePicker
                    style={{ width: '85%' }}
                    // showTime
                    format="YYYY-MM-DD"
                    placeholder="选择平仓日期"
                  />
                )}
              </FormItem>
              <FormItem label="平仓价格" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                {getFieldDecorator('closePositionPrice', { initialValue: '', })(
                  <Input style={{ width: '85%' }} placeholder="平仓价格" />
                )}
              </FormItem>
            </Col>
          </Row>
        </Form>
      </Modal>
    );
  }

  componentDidMount() {}

  componentWillUnmount() {}
}

export default AddPositionModal;
